package top.dfghhj.anhanx.oauth.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.dfghhj.anhanx.oauth.entity.DO.SysUser;
import top.dfghhj.anhanx.oauth.service.SysUserService;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private SysUserService sysUserService;

    @PreAuthorize("hasRole('read')")
    @GetMapping("/{username}")
    public List<SysUser> findByUsername(@PathVariable String username) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(JSONObject.toJSONString(authentication.getPrincipal()));
        return sysUserService.findByUsername(username);
    }

}
